package com.py.androidUtils.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.py.androidUtils.base.BaseAction.TaskListener;
import com.py.androidUtils.pulltorefrsh.PullToRefreshScrollView;
import com.py.androidUtils.utils.AppUtils;
import com.py.androidUtils.view.LoadDataEemptyView;
import com.py.androidUtils.view.LoadDataEemptyView.IClickLoadListener;
import com.readyidu.app.R;

/**
 * @说明 详情界面
 * @作者 LY
 * @文件 BaseDetailsFragment.java
 * @时间 2015年7月13日 下午1:42:18
 * @版权 Copyright(c) 2015 LY-版权所有
 */
public abstract class BaseDetailsFragment extends BaseFragment {
	private PullToRefreshScrollView scrollView;
	private LoadDataEemptyView loadData;
	public BaseAction baseAction;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.common_base_details_fragment, container, false);
		scrollView = (PullToRefreshScrollView) view.findViewById(R.id.scrollView);
		loadData = (LoadDataEemptyView) view.findViewById(R.id.loadData);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		onActivityCreatedCallBack();
		View detailsView = getDetailsView();
		if (detailsView != null) {
			scrollView.addView(detailsView);
		}
		baseAction = new BaseAction(getActivity()) {
			@Override
			protected void doInbackground() {
				doInbackgroundCallBack();
			}
		};
		baseAction.setTaskListener(new TaskListener() {
			@Override
			public void onPreExecute() {
				onPreExecuteCallBack();
			}

			@Override
			public void onPostExecute() {
				Object object = baseAction.getData();
				if (object != null) {
					loadData.setVisibility(View.GONE);
					scrollView.setVisibility(View.VISIBLE);
					doHandelData(object);
				} else {
					loadData.setVisibility(View.VISIBLE);
					scrollView.setVisibility(View.GONE);
					loadData.loadEmptyError();
				}
				stopRefreshView();
			}
		});
		checkNetWorkLoadData();

		loadData.setClickLoadListener(new IClickLoadListener() {
			@Override
			public void load() {
				checkNetWorkLoadData();
			}

			@Override
			public void settingNetWork() {
				AppUtils.settingNetWork(getActivity());
			}
		});
	}

	/**
	 * 检测网络网络连接正常则加载数据
	 */
	public void checkNetWorkLoadData() {
		scrollView.setVisibility(View.GONE);
		loadData.setVisibility(View.VISIBLE);
		loadData.loadData();
		loadData();

	}

	/**
	 * 请求数据
	 */
	private void loadData() {
		baseAction.execute();
	}

	/**
	 * 结束刷新
	 */
	public void stopRefreshView() {
		scrollView.onRefreshComplete();
	}

	/** 界面回调 */
	public abstract void onActivityCreatedCallBack();

	/** 获取详情界面 */
	public abstract View getDetailsView();

	/** 数据回调 */
	public abstract void doInbackgroundCallBack();

	public abstract void onPreExecuteCallBack();

	/** 绑定数据 */
	public abstract void doHandelData(Object object);
}
